﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormApplication.FormBase;
using WindowsFormsView;
using WindowsFormsPresenter.RestaurantArea;
using WindowsFormsModel.BusinessObjects;

namespace WindowsFormApplication.FormCategory
{
    public partial class frmRestaurantAreaList : frmBaseCategory, IRestaurantAreasView
    {
        private RestaurantAreasPresenter _restaurantAreasPresenter;

        private static int totalPage = 0;
        private static int currentPage = 0;

        public IList<RestaurantAreaModel> RestaurantAreas
        {
            set
            {
                var restaurantAreas = value;
                grdMain.DataSource = restaurantAreas;
                for (int i = 0; i < grdMain.DisplayLayout.Bands[0].Columns.Count; i++) { grdMain.DisplayLayout.Bands[0].Columns[i].Hidden = true; }
                grdMain.DisplayLayout.Bands[0].Columns["RestaurantAreaCode"].Hidden = false;
                grdMain.DisplayLayout.Bands[0].Columns["RestaurantAreaName"].Hidden = false;
                grdMain.DisplayLayout.Bands[0].Columns["Description"].Hidden = false;
                grdMain.DisplayLayout.Bands[0].Columns["IsActive"].Hidden = false;

                grdMain.DisplayLayout.Bands[0].Columns["RestaurantAreaCode"].Header.Caption = "Mã khu vực nhà hàng";
                grdMain.DisplayLayout.Bands[0].Columns["RestaurantAreaName"].Header.Caption = "Tên khu vực nhà hàng";
                grdMain.DisplayLayout.Bands[0].Columns["Description"].Header.Caption = "Diễn giải";
                grdMain.DisplayLayout.Bands[0].Columns["IsActive"].Header.Caption = "Được sử dụng";
            }
        }

        protected override void InitData()
        {
            try
            {
                totalPage = _restaurantAreasPresenter.GetTotalPageByBranch();
                _restaurantAreasPresenter.DisplayByPageAndBranch(currentPage);
                toolbarsManager.Tools["lblPage"].SharedProps.Caption = "Số trang " + (currentPage + 1) + "/ " + (totalPage + 1);
            }
            catch (ApplicationException ex) { ShowMessageBox(ex.Message, "Thông báo lỗi từ hệ thống !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        protected override void NextPage()
        {
            if (currentPage < totalPage)
            {
                currentPage = currentPage + 1;
                InitData();
            }
        }

        protected override void PreviousPage()
        {
            if (currentPage > 0)
            {
                currentPage = currentPage - 1;
                InitData();
            }
        }

        protected override void DeleteRow()
        {
            try
            {
                if (grdMain.ActiveRow.Index >= 0)
                {
                    string restaurantAreaId = grdMain.ActiveRow.Cells[TablePrimaryKey].Value.ToString();
                    new RestaurantAreaPresenter(null).Delete(restaurantAreaId);
                    currentPage = 0;
                    ShowMessageBox("Xóa dữ liệu hoàn thành !", "Thông báo xóa dữ liệu !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    ShowMessageBox("Bạn chưa chọn dòng để xóa !", "Thông báo xóa dữ liệu !", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (ApplicationException ex) { ShowMessageBox(ex.Message, "Thông báo lỗi hệ thống !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        public frmRestaurantAreaList()
        {
            InitializeComponent();

            _restaurantAreasPresenter = new RestaurantAreasPresenter(this);
        }
    }
}
